package com.util.auth.impl;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

import com.util.auth.DecodeFactory;

public class DecodeFactoryImplBase64Impl
	implements DecodeFactory
{
  public String decode(String url, String key)
    throws UnsupportedEncodingException
  {
    String urlTemp = url;
    byte[] bin = Base64.decodeBase64(urlTemp.getBytes("utf-8"));

    byte[] bout = new byte[bin.length];
    byte[] bkey = key.getBytes();
    for (int i = 0; i < bin.length; i++) {
      bout[i] = (byte)(bin[i] - bkey[(i % bkey.length)]);
    }
    return new String(bout, 0, bout.length, "utf-8");
  }

  public String encode(String url, String key)
    throws UnsupportedEncodingException
  {
    byte[] bin = url.getBytes("utf-8");
    byte[] bout = new byte[bin.length];
    byte[] bkey = key.getBytes();
    for (int i = 0; i < bin.length; i++) {
      bout[i] = (byte)(bin[i] + bkey[(i % bkey.length)]);
    }

    String stringPlayLink = new String(Base64.encodeBase64(bout));
    return stringPlayLink;
  }

  static String GetDelimiter() {
    return "\r\n";
  }
}
